package zhao.tx.eecs.Tools;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.andware.volley.Request;
import com.andware.volley.RequestQueue;
import com.andware.volley.Response;
import com.andware.volley.VolleyError;
import com.andware.volley.toolbox.GsonRequest;
import com.andware.volley.toolbox.SubStringRequest;

import java.util.List;

import static com.andware.volley.Response.ErrorListener;

/**
 * Created by ishishuji on 14-7-10.
 */
public class VolleyTokenBase extends VolleyBase{


    public static <T> Request volleyPostBase ( String url , T requestValue , String token , final Handler handler ) {
        if ( baseQueue != null ) {
            SubStringRequest postRequest = new SubStringRequest ( Request.Method.POST ,url, requestValue ,new Response.Listener<String> () {
                @Override
                public void onResponse ( String response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            postRequest.setToken ( token );
            baseQueue.add ( postRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return postRequest;
        }
        return null;
    }

    public static <T> Request volleyPostNullBase ( String url , String token , final Handler handler ) {
        if ( baseQueue != null ) {
            SubStringRequest postRequest = new SubStringRequest ( Request.Method.POST ,url,new Response.Listener<String> () {
                @Override
                public void onResponse ( String response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            postRequest.setToken ( token );
            baseQueue.add ( postRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return postRequest;
        }
        return null;
    }

    public static <T> Request  volleyPutBase ( String url , T requestValue ,String token, final Handler handler ) {
        if ( baseQueue != null ) {
            SubStringRequest putRequest = new SubStringRequest ( Request.Method.PUT ,url, requestValue ,new Response.Listener<String> () {
                @Override
                public void onResponse ( String response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            putRequest.setToken ( token );
            baseQueue.add ( putRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return putRequest;
        }
        return null;
    }

    //add token

    public static <T> Request  volleySameClsPost ( String url , T requestValue , String token , Class<T> cls , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest postRequets = new GsonRequest ( Request.Method.POST ,url, requestValue , cls ,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            postRequets.setToken ( token );
            baseQueue.add ( postRequets );
            handler.obtainMessage ( START ).sendToTarget ();
            return postRequets;
        }
        return null;
    }

    public static <T,U> Request  volleyPost ( String url , T requestValue , Class<U> cls , String token , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest postRequest = new GsonRequest ( Request.Method.POST ,url, requestValue , cls ,new Response.Listener<U> () {
                @Override
                public void onResponse ( U response ) {
                    handler.obtainMessage ( SUCCESS,response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD,error ).sendToTarget ();
                }
            });
            postRequest.setToken ( token );
            baseQueue.add ( postRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return postRequest;
        }
        return null;
    }

    public static <T> Request  volleySameClsPut ( String url , T requestValue , String token , Class<T> cls , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest putRequest = new GsonRequest ( Request.Method.PUT ,url, requestValue , cls ,new Response.Listener<T> () {
                @Override
                public void onResponse ( T response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            putRequest.setToken ( token );
            baseQueue.add ( putRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return putRequest;
        }
        return null;
    }

    public static <T,U> Request  volleyPut ( String url , T requestValue , Class<U> cls , String token , final Handler handler ) {
        if ( baseQueue != null ) {
            GsonRequest putRequest = new GsonRequest ( Request.Method.PUT ,url, requestValue , cls ,new Response.Listener<U> () {
                @Override
                public void onResponse ( U response ) {
                    handler.obtainMessage ( SUCCESS , response ).sendToTarget ();
                }
            } , new ErrorListener () {
                @Override
                public void onErrorResponse ( VolleyError error ) {
                    handler.obtainMessage ( FAILD , error ).sendToTarget ();
                }
            });
            putRequest.setToken ( token );
            baseQueue.add ( putRequest );
            handler.obtainMessage ( START ).sendToTarget ();
            return putRequest;
        }
        return null;
    }

}
